$(function() {
	BUcontrast = 1;
	getBUcontrast()
})

function getBUcontrast(){
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	$.get(
		`${API_URL}getdata?budgetYear=${currYear}&status=2&chartPage=60101&chartNo=4&username=${getUsername}`, {},
		function(res) {
			console.log(res)
			let re = eval("(" + res + ")");
			console.log(re)
			getDepartment(re.items[0].charts[0].data)
		}
	)
	getSubtaskData()
}

function getDepartment(data) {
	let yusuanData = [];
	let shijiData = [];
	let legendData = ['成本预算', '实际'];
	let xData = [];
	for (let i = 0; i < data.length; i++) {
		xData.push(data[i].key01);
		yusuanData.push(FormatWYCash(data[i].key02));
		shijiData.push(FormatWYCash(data[i].key03));
		
	}
	
	let seriesData = [{
			name: "成本预算",
			barWidth: "18%",
			type: "bar",
			data: yusuanData,
			itemStyle: {
				color: "#F7BA1E",
			},
			label: {
				show: true,
				position: 'top',
				fontSize: 12,
				color: "#1D2129",
				fontWeight:600,
			},
		},
		{
			name: "实际",
			barWidth: "18%",
			type: "bar",
			data: shijiData,
			itemStyle: {
				color: "#20C997",
			},
			label: {
				show: true,
				position: 'top',
				fontSize: 12,
				color: "#1D2129",
				fontWeight:600,
			},
		}];
	have("department") ? monthTrend("department", seriesData, legendData,xData, 123) : "";
	
}


var http = 'http://www.esunrising.net:8099/ords/kzks/api/';
var searchVal = '';
var page = 1;
let limit = 10;
let tableHeight = $('.container .detail').height() - $('.container .detail .box .title').height() - 72;
let cols = [
	[{
			field: 'key09',
			title: '排名',
			sort: true
			// templet: function(d) {
			// 	return `<div class="handlers">
			// 	<img src="img/equipment-situation/Avatar.svg" >
			// 	<p>${d.PROJECT_NAME}</p>
			// </div>`
			// }
		},
		{
			field: 'key01',
			title: '事业部',
			templet: function(d) {
				return `<h6>${d.key01}</h6>`
			}
			
		},
		{
			field: 'key02',
			title: '项目个数',
			align:'right',
			sort: true
		},
		{
			field: 'key03',
			title: '预算',
			align:'right',
			sort: true,
			templet: function(d) {
				return `<h6>${FormatWYCash(d.key03)}</h6>`
			}
		},
		{
			field: 'key04',
			title: '实际',
			align:'right',
			sort: true,
			templet: function(d) {
				return `<h6>${FormatWYCash(d.key04)}</h6>`
			}
		},
		{
			field: 'key05',
			title: '差额',
			align:'right',
			sort: true,
			templet: function(d) {
				return `<h6 class="${d.key05<0?'minus':'plus'}">${FormatWYCash(d.key05)}</h6>`
			}
		},
		// {
		// 	field: 'key06',
		// 	title: '超预算上限项目个数',
		// 	minWidth:160,
		// 	align:'right',
		// 	sort: true
		// },
		// {
		// 	field: 'key07',
		// 	title: '低于预算下限项目个数',
		// 	minWidth:170,
		// 	align:'right', 
		// 	sort: true
		// },
		// {
		// 	field: 'key08',
		// 	title: '预算内项目个数',
		// 	minWidth:120,
		// 	align:'right',
		// 	sort: true
		// }
	]
];

function getSubtaskData() {
	var data = [];
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	$.ajax({
		type: 'get',
		url: `${API_URL}getdata?budgetYear=${currYear}&status=2&chartPage=60101&chartNo=5&username=${getUsername}`,
		data: {
			// page_num: page,
			// search: searchVal,
			// limit_num: limit
		},
		dataType: "json",
		async: true,
		success: function(res) {
			console.log(res)
			let re = res;
			let data = re.items[0].charts[0].data ? re.items[0].charts[0].data : [];
			subtaskTableDrawing(data) // 数据传到 table组件
			// if (res.code == 200) {
				
			// } else {
			// 	console.log(res);
			// }
		},
		error: function() {
			console.log('访问失败');
		}
	});
}

function subtaskTableDrawing(data) {
	layui.use('table', function() {
		let table = layui.table;
		table.render({
			elem: '#particulars',
			cols: cols,
			data: data, // 数据
			height: tableHeight,
		});
	})
}